package com.chan.net;

import android.arch.lifecycle.Observer;
import android.net.ParseException;
import android.support.annotation.Nullable;
import android.support.v4.util.Pools;

import com.base.pool.FactoryPools;
import com.base.pool.StateVerifier;

import org.json.JSONException;

import java.io.InterruptedIOException;
import java.net.ConnectException;
import java.net.UnknownHostException;

import retrofit2.HttpException;

/**
 * 仿glide的对象池封装的Observer对象池
 */
public class DefaultLiveDataObserver implements Observer<Result>, FactoryPools.Poolable {

    private HttpCallback httpCallback;
    private String requestCommand;
    private final StateVerifier stateVerifier = StateVerifier.newInstance();

    private static final Pools.Pool<DefaultLiveDataObserver> POOL = FactoryPools.simple(10,
            new FactoryPools.Factory<DefaultLiveDataObserver>() {
                @Override
                public DefaultLiveDataObserver create() {
                    return new DefaultLiveDataObserver();
                }
            });

    public static DefaultLiveDataObserver obtain() {
        DefaultLiveDataObserver observer = POOL.acquire();
        if (observer == null) {
            observer = new DefaultLiveDataObserver();
        }
        return observer;
    }

    /**
     * 对象池重用
     *
     * @param requestCommand
     * @param httpCallback
     * @return
     */
    public static DefaultLiveDataObserver obtain(String requestCommand, HttpCallback httpCallback) {
        DefaultLiveDataObserver observer = obtain();
        observer.init(requestCommand, httpCallback);
        return observer;
    }

    private synchronized void init(String requestCommand, HttpCallback httpCallback) {
        this.httpCallback = httpCallback;
        this.requestCommand = requestCommand;
    }

    /**
     * 清空销毁
     */
    private synchronized void recycle() {
        httpCallback = null;
        requestCommand = null;
        POOL.release(this);
    }

    private DefaultLiveDataObserver() {

    }

    @Override
    public void onChanged(@Nullable Result t) {
        try {
            if (t.code == 0) {
                if (httpCallback != null) {
                    if (t.data != null) {
                        httpCallback.onSuccess(requestCommand, t);
                    } else {
                        httpCallback.onFail(requestCommand, Result.FAILCODE.CODE_DATA_EMPTY, "数据为null");
                    }
                }
            } else {
                if (httpCallback != null) {
                    httpCallback.onFail(requestCommand, t.code, t.message);
                }
            }
            recycle();
        } catch (Exception e) {
            int code;
            String msg;
            if (e instanceof HttpException) {     //   HTTP错误
                code = Result.FAILCODE.CODE_BAD_NETWORK;
                msg = "HTTP错误";
            } else if (e instanceof ConnectException
                    || e instanceof UnknownHostException) {   //   连接错误
                code = Result.FAILCODE.CODE_CONNECT_ERROR;
                msg = "连接错误";
            } else if (e instanceof InterruptedIOException) {   //  连接超时
                code = Result.FAILCODE.CODE_CONNECT_TIMEOUT;
                msg = "连接超时";
            } else if (e instanceof JSONException
                    || e instanceof ParseException) {   //  解析错误
                code = Result.FAILCODE.CODE_PARSE_ERROR;
                msg = "解析错误";
            } else {
                code = Result.FAILCODE.CODE_UNKNOWN_ERROR;
                msg = "位置错误";
            }
            if (httpCallback != null) {
                httpCallback.onFail(requestCommand, code, msg);
            }
        }
    }


    @Override
    public StateVerifier getVerifier() {
        return stateVerifier;
    }
}
